package ilvonen.ihstatistics;

import ilvonen.ihstatistics.models.ConstantData;
import ilvonen.ihstatistics.models.Team;
import ilvonen.ihstatistics.repository.DBAdapter;
import ilvonen.ihstatistics.repository.DBLeagues;
import ilvonen.ihstatistics.repository.DBSeasonTypes;
import ilvonen.ihstatistics.repository.DBSeasons;
import ilvonen.ihstatistics.repository.DBTeams;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

public class NewGameSettings extends Activity {

	/*
	 * DB-Classes needed for database operations
	 */
		private DBAdapter db = new DBAdapter(this);
		private DBTeams DBTeamFuncions = new DBTeams();
		private DBSeasons DBSeasonFunctions = new DBSeasons();
		private DBSeasonTypes DBSeasonTypeFunctions = new DBSeasonTypes();
		private DBLeagues DBLeagueFunctions = new DBLeagues();
	
	/*
	 * Object lists. Database functions return object lists.
	 */
		private List<Team> _teamList = new ArrayList<Team>();
		private List<ConstantData> _seasonList = new ArrayList<ConstantData>();
		private List<ConstantData> _seasonTypeList = new ArrayList<ConstantData>();
		private List<ConstantData> _leagueList = new ArrayList<ConstantData>();
	
	/*
	 *  Spinners that need to be populated
	 */
		private Spinner s1;
		private Spinner s2;
		private Spinner s3;
		private Spinner s4;
	
	/*
	 * Lists that contain the information for the spinners.
	 */
		private List<String> teams = new ArrayList<String>();
		private List<String> seasons = new ArrayList<String>();
		private List<String> seasontypes = new ArrayList<String>();
		private List<String> leagues = new ArrayList<String>();
	
	/*
	 * used for identifying teams
	 */
		private int accountId;
	
	/*
	 * For setting the date of the game.
	 */
		private DatePicker mGameDate;
		private String date;
		private int mYear;
		private int mMonth;
		private int mDay;
	
	/*
	 * For getting the name of the opponent
	 */
		
		private EditText opponentNameEditor;
	/*
	 * Start button.
	 */
		private Button btnStartGame;
	
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_game_settings);
		s1 = (Spinner)findViewById(R.id.spinTeams);
		s2 = (Spinner)findViewById(R.id.spinSeasons);
		s3 = (Spinner)findViewById(R.id.spinSeasonTypes);
		s4 = (Spinner)findViewById(R.id.spinLeagues);
		/*
		 * Get extras sent from mainactivity
		 */
		
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			accountId = extras.getInt("accountId");
		}
		
		
		opponentNameEditor = (EditText)findViewById(R.id.editOpponentName);
		SetDatePicker();
		PopulateSpinners();
		
		btnStartGame = (Button)findViewById(R.id.btnStartNewGame);
		btnStartGame.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// get the date from the datepicker
				mYear = mGameDate.getYear();
				mMonth = mGameDate.getMonth()+1;
				mDay = mGameDate.getDayOfMonth();
				date = mDay + "." + mMonth + "." + mYear;
				
				//get id that was set by selecting from spinners
				int teamid = _teamList.get(s1.getSelectedItemPosition()).get_id();
				int seasonid = _seasonList.get(s2.getSelectedItemPosition()).get_id();
				int seasontypeid = _seasonTypeList.get(s3.getSelectedItemPosition()).get_id();
				int leagueid = _leagueList.get(s4.getSelectedItemPosition()).get_id();
				
				//Create new activity and pass information received in this activity.
				Intent roster = new Intent(getApplication(), RosterActivity.class);
				roster.putExtra("accountId", accountId);
				roster.putExtra("teamid", teamid);
				roster.putExtra("seasonid", seasonid);
				roster.putExtra("seasontypeid", seasontypeid);
				roster.putExtra("leagueid", leagueid);
				roster.putExtra("opponent", opponentNameEditor.getText().toString());
				roster.putExtra("date", date);
				//start new activity.
				startActivity(roster);
			}
		});
	}

	private void PopulateSpinners() {	
		/*
		 * Fetch information from the database needed for the spinners and populate the spinners.
		 * Remember to open and close the database.
		 */
		db.open();
		_teamList = DBTeamFuncions.getAllTeams(db);
		_seasonList = DBSeasonFunctions.getAllSeasons(db);
		_seasonTypeList = DBSeasonTypeFunctions.getAllSeasonTypes(db);
		_leagueList = DBLeagueFunctions.getAllLeagueTypes(db);
		db.close();
		
		/*
		 * Go through the lists and add names (strings) to right string lists that are used to populate the spinners.
		 */
		if(_teamList != null){
			for(Team t : _teamList){
				teams.add(t.get_name());
			}
		}
		if(_seasonList != null){
			for(ConstantData s : _seasonList){
				seasons.add(s.get_name());
			}
		}
		if(_seasonTypeList != null){
			for(ConstantData st : _seasonTypeList){
				seasontypes.add(st.get_name());
			}
		}
		if(_leagueList != null){
			for(ConstantData l : _leagueList){
				leagues.add(l.get_name());
			}
		}
		
		//add string list of teams to spinner no. 1
		ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, teams);
		dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s1.setAdapter(dataAdapter1);
		
		//add string list of seasons to spinner no. 2
		ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, seasons);
		dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s2.setAdapter(dataAdapter2);
		
		//add string list of season types to spinner no. 3
		ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, seasontypes);
		dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s4.setAdapter(dataAdapter3);
		
		//add string list of leagues to spinner no. 4
		ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, leagues);
		dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s3.setAdapter(dataAdapter4);
	}

	private void SetDatePicker() {
		// TODO Auto-generated method stub
		mGameDate = (DatePicker)findViewById(R.id.dpGameDate);
		mGameDate.setCalendarViewShown(true);//show spinners only
		mGameDate.setSpinnersShown(false);
		mGameDate.getCalendarView().setFirstDayOfWeek(Calendar.MONDAY);
	}
	
	
}